<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$keywords=$this->plugin_setting('search');
$return['params']=array('jy'=>1);
if($_RQ['op']=='main'){
	//下面list,PC端需要获取服务列表
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
	);
	$searchkeyword=$return['pluginset']['keyword'];
	$return['searchkeyword']=explode(';',$searchkeyword);
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
		$return['params']['ccate']=intval($_RQ['cid']);
	}
	
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pid']){
		$params['pcate']=intval($_RQ['pid']);
		$return['params']['pid']=intval($_RQ['pid']);
	}else{
		$return['params']['pid']=0;
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	if(trim($_RQ['pluginname'])){
		$params['pluginname']=trim($_RQ['pluginname']);
		$return['params']['pluginname']=trim($_RQ['pluginname']);
	}
	$params['onepluginname']='jy_mfuwu';
	//进一步整理筛选条件，服务属性字段是否需 要调整，服务分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit');
	$orderby='paixu desc, star desc, id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
			
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	//PC端先关闭获取，教给JS
	if($_SESSION['os']=='windows' && false){
		if($_RQ['order']=='sale_offer'){
			$list=model('jy_weishop/activity/sales')->getoffer();
		}else{
			$list=model('jy_weishop/goods')->goods_list($parameter,$total);
		}
	}
	$return['list']=array();
	$return['order']=$_RQ['order'];
	//最近浏览
	$params=array(
		//'table'=>'jy_weishop_member_history',
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),
		'fields'=>array('id','goodsid','merchid','createtime'),
		'orderby'=>'createtime desc',
		'limit'=>'5',
	);
	if($_SESSION['os']=='windows'){
		$hlist=pdo_getlist('jy_weishop_member_history',$params);
	}
	if($hlist){
		foreach($hlist as &$row){
			$goods=model('jy_weishop/goods')->goods_detail($row['goodsid'],array('title','image','price'));
			$row=array_merge($goods,$row);
		}
	}
	$return['hlist']=$hlist;
	$groupparams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$groupparams['onepluginname']='jy_mfuwu';
	$category=pdo_getall('jy_weishop_goods_group',$groupparams,array(),'id','paixu desc');
	foreach($category as &$item){
		$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'id','paixu desc');
	}
	//推荐分类
	$groupparams['isrecommand']=1;
	unset($groupparams['parentid']);
	$recommend=pdo_getall('jy_weishop_goods_group',$groupparams);
	$return['recommend']=$return['childs']=$recommend;
	$return['category']=$category;
	if($_RQ['pcate'] || $_RQ['pid']){
		$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
		$return['params']['pcate']=$pcate;
	}
	if($_RQ['ccate'] || $_RQ['cid']){
		$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
		$return['params']['ccate']=$ccate;
	}
	$return['catlevel']=2;
	$return['opencategory']=true;
	$return['feemoney']=$this->plugin_setting('feemoney');
	set_html('title',$return['shopset']['title']?:'服务列表');
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?'全部服务'.$return['pluginset']['share_title']:'全部服务-在线维修',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'在线维修，一个属于自己的在线维修！',
		'link'=>custom_url('jy_mfuwu/app.goods.index//fromuser='.$_SESSION['uid'].'&merchid='.$_SESSION['merchid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['share_image']
	);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'type'=>4,
		'merchid'=>$_SESSION['merchid'],
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}else{
		//有关键词时，分类ID不检索！
		if(($_RQ['pcate'] || $_RQ['pid']) && $_RQ['pid']!=-1){
			$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
			$params['pcate']=$pcate;
			$return['params']['pcate']=$pcate;
		}
		if($_RQ['ccate'] || $_RQ['cid']){
			$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
			$params['ccate']=$ccate;
			$return['params']['ccate']=$ccate;
		}
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}elseif($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}elseif($_RQ['order']=='ishot' && !$_RQ['ccate'] && !$_RQ['pcate']){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}else{
		$return['params']['order']=$_RQ['order'];
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	if($_RQ['isvideogoods'] || $_RQ['order']=='isvideogoods'){
		$params['video <>']='';
		$return['params']['isvideogoods']=intval($_RQ['isvideogoods']);
	}
	if($_RQ['star']){
		$params['star >=']=intval($_RQ['star']);
		$return['params']['star']=intval($_RQ['star']);
	}
	//电商客服务
	$params['onepluginname']='jy_mfuwu';
	//进一步整理筛选条件，服务属性字段是否需 要调整，服务分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_SESSION['os']=='windows'?12:10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','api_data');
	if($_RQ['wxappjiaoyi']){
		$fields[]='api_id';
		$params['api_id >']=0;
		$params['api_pluginname']='jy_wxappjiaoyib';
	}
	$orderby='paixu desc, star desc,  salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc, paixu desc';
		}
		if($_RQ['order']=='new'){
			$orderby='id desc, paixu desc';
		}
		if($_RQ['order']=='star'){
			$orderby='star desc, paixu desc';
		}
		if($_RQ['order']=='you'){
			$orderby='salenum desc, paixu desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	if(($_RQ['order']=='sale_limit' || $_RQ['order']=='sale_presell'  || $_RQ['order']=='sale_cuxiao' || $_RQ['order']=='sale_offer' || $_RQ['order']=='sale_new' || $_RQ['order']=='sale_libao') && !$return['params']['pcate'] && !$return['params']['ccate']){
		//if(intval($_RQ['page'])==1){
			if( $_RQ['order']=='sale_offer'){
				$getoffer=model('jy_weishop/activity/sales')->getoffer();
				$list=$getoffer['list'];
			}
			if( $_RQ['order']=='sale_cuxiao'){
				$list=model('jy_weishop/activity/sales')->goods_list(500);
			}
			if( $_RQ['order']=='sale_limit'){
				$list=model('jy_weishop/activity/limit')->goods_list(500);
			}
			if( $_RQ['order']=='sale_presell'){
				$list=model('jy_weishop/activity/presell')->goods_list(500);
			}
			if( $_RQ['order']=='sale_new'){
				$sale_new=model('jy_weishop/activity/sales')->getnew();
				$list=$sale_new['list'];
			}
			if( $_RQ['order']=='sale_libao' && $_RQ['sale_libaoid']){
				$return['params']['sale_libaoid']=$_RQ['sale_libaoid'];
				$sale_libao=model('jy_weishop/activity/sales')->getlibao($_RQ['sale_libaoid']);
				$list=$sale_libao['list'];
			}
			$ids=array();
			foreach($list as &$ov){
				$ids[$ov['id']]=$ov['id'];
			}
			if(count($ids)>0){
				$ids=array_keys($ids);
			}else{
				$ids=array(0);
			}
			$params['id']=$ids;
			$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
			);
			$list=model('jy_weishop/goods')->goods_list($parameter,$total);
		//}
	}else{
		$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
		);
		$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	}
	if(!$list){
		exi('none');
	}else{
		$feemoneyset=$this->plugin_setting('feemoney');
		$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
		foreach($list as &$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
			if($feemoneyset['isopen']==1){
				$itemv['feemoney']=$itemv['marketprice']>0?$itemv['marketprice']-$itemv['price']:$feemoneyset['feerade']*$itemv['price']/100;
				$itemv['feemoney']=price($itemv['feemoney']);
			}
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			if($this->plugin_exists('jy_merch')){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
			$itemv['api_data']=json_decode($itemv['api_data'],true);
		}
	}
	exi($list);
}
if($_RQ['op']=='search'){
	$setting=$this->plugin_setting('setting');
	$return['setting']=$setting;
	$return['shopset']=$this->plugin_setting('shopset');
	$searchkeyword=$this->plugin_setting('search')['keyword'];
	$return['searchkeyword']=explode(';',$searchkeyword);
	$return['list']=array();
	$return['params']=array('keyword'=>'');
	$return['indexpai']=array();
	if($return['setting']['issearch']){
		$return['indexpai'][]=array('name'=>'search');
	}
	if($return['setting']['ishdps']){
		$return['indexpai'][]=array('name'=>'hdps');
	}
	if($return['setting']['isbankuai1']){
		$return['indexpai'][]=array('name'=>'bankuai1');
	}
	if($return['setting']['bankuai2']){
		$return['indexpai'][]=array('name'=>'bankuai2');
	}
	if($return['setting']['cube']){
		$return['indexpai'][]=array('name'=>'cube');
	}
	if($return['setting']['share']){
		$return['indexpai'][]=array('name'=>'share');
	}
	$return['indexpai'][]=array('name'=>'goods');
	//推荐服务
	$indexrecommands=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'saletype like'=>'%;1;%'),array('id','title','image','price','total'));
	$return['indexrecommands']=$indexrecommands;
	set_html('title',$setting['title']?'搜索-'.$setting['title']:'搜索-在线商城');
	exi($return);
}	
?>